package com.agritech.path;

import java.util.ArrayList;
import java.util.List;

/**
 * 路径规划结果类
 * 用于存储路径规划的结果，包括路径点、总距离、预计时间等信息
 */
public class PathPlan {
    private List<PathPoint> pathPoints; // 路径点列表
    private double totalDistance; // 总距离(米)
    private double estimatedTime; // 预计时间(分钟)
    private String planningMethod; // 规划方法
    private boolean hasObstacles; // 是否包含障碍物
    
    public PathPlan() {
        this.pathPoints = new ArrayList<>();
        this.totalDistance = 0.0;
        this.estimatedTime = 0.0;
        this.planningMethod = "未知";
        this.hasObstacles = false;
    }
    
    /**
     * 添加路径点
     * @param point 路径点
     */
    public void addPathPoint(PathPoint point) {
        pathPoints.add(point);
        recalculateDistance();
    }
    
    /**
     * 重新计算总距离
     */
    private void recalculateDistance() {
        if (pathPoints.size() < 2) {
            totalDistance = 0.0;
            return;
        }
        
        totalDistance = 0.0;
        for (int i = 0; i < pathPoints.size() - 1; i++) {
            PathPoint current = pathPoints.get(i);
            PathPoint next = pathPoints.get(i + 1);
            totalDistance += calculateDistance(current, next);
        }
        
        // 假设平均速度为5米/秒
        estimatedTime = totalDistance / 5.0 / 60.0;
    }
    
    /**
     * 计算两点之间的距离
     * @param p1 第一个点
     * @param p2 第二个点
     * @return 两点之间的距离
     */
    private double calculateDistance(PathPoint p1, PathPoint p2) {
        return Math.sqrt(Math.pow(p2.getX() - p1.getX(), 2) + Math.pow(p2.getY() - p1.getY(), 2));
    }
    
    /**
     * 打印路径规划结果
     */
    public void printPathPlan() {
        System.out.println("路径规划结果:");
        System.out.println("规划方法: " + planningMethod);
        System.out.println("总距离: " + String.format("%.2f", totalDistance) + "米");
        System.out.println("预计时间: " + String.format("%.2f", estimatedTime) + "分钟");
        System.out.println("路径点数量: " + pathPoints.size());
        System.out.println("是否包含障碍物: " + (hasObstacles ? "是" : "否"));
        
        if (pathPoints.size() <= 10) {
            System.out.println("路径点详情:");
            for (int i = 0; i < pathPoints.size(); i++) {
                PathPoint point = pathPoints.get(i);
                System.out.println("  点" + (i + 1) + ": (" + point.getX() + ", " + point.getY() + ")");
            }
        } else {
            System.out.println("起点: (" + pathPoints.get(0).getX() + ", " + pathPoints.get(0).getY() + ")");
            System.out.println("终点: (" + pathPoints.get(pathPoints.size() - 1).getX() + ", " + 
                    pathPoints.get(pathPoints.size() - 1).getY() + ")");
            System.out.println("中间路径点较多，不全部显示");
        }
    }
    
    // Getters and Setters
    public List<PathPoint> getPathPoints() {
        return pathPoints;
    }
    
    public void setPathPoints(List<PathPoint> pathPoints) {
        this.pathPoints = pathPoints;
        recalculateDistance();
    }
    
    public double getTotalDistance() {
        return totalDistance;
    }
    
    public double getEstimatedTime() {
        return estimatedTime;
    }
    
    public void setEstimatedTime(double estimatedTime) {
        this.estimatedTime = estimatedTime;
    }
    
    public String getPlanningMethod() {
        return planningMethod;
    }
    
    public void setPlanningMethod(String planningMethod) {
        this.planningMethod = planningMethod;
    }
    
    public boolean isHasObstacles() {
        return hasObstacles;
    }
    
    public void setHasObstacles(boolean hasObstacles) {
        this.hasObstacles = hasObstacles;
    }
}